package com.yoyo.itravel.service.impl;

import com.yoyo.itravel.bean.Sight;
import com.yoyo.itravel.bean.User;
import com.yoyo.itravel.constants.DateFormatConstant;
import com.yoyo.itravel.dao.ISightCollectDao;
import com.yoyo.itravel.dao.ISightDao;
import com.yoyo.itravel.dao.IUserDao;
import com.yoyo.itravel.exceptions.SightCollectException;
import com.yoyo.itravel.service.ISightCollectService;
import com.yoyo.itravel.utils.DateUtils;
import com.yoyo.itravel.utils.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 用户表-景点表中间表：用户收藏景点服务层接口实现类
 *
 * @Author HZX
 * @Date 2020/5/20 22:39
 */
@Service
public class SightCollectServiceImpl implements ISightCollectService {

    @Autowired
    private ISightCollectDao sightCollectDao;
    @Autowired
    private ISightDao sightDao;
    @Autowired
    private IUserDao userDao;

    /**
     * @Description 插入收藏数据
     *
     * @Param sightCollect
     * @Return boolean
     */
    @Override
    public boolean addCollect(String userId, String sightId, Date collectTime) throws SightCollectException {
        if (StringUtils.isEmpty(userId, sightId)) {
            throw new SightCollectException("景点id或收藏id为空！");
        }
        try {
            return sightCollectDao.insert(userId, sightId, collectTime) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCollectException("插入收藏数据失败！");
        }
    }

    /**
     * @Description 通过用户id和景点id删除景点收藏数据
     *
     * @Param userId
     * @Param sightId
     * @Return boolean
     */
    @Override
    public boolean deleteCollect(String userId, String sightId) throws SightCollectException {
        if (StringUtils.isEmpty(userId, sightId)) {
            throw new SightCollectException("景点id或收藏id为空！");
        }
        try {
            return sightCollectDao.deleteByUserIdAndSightId(userId, sightId) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCollectException("通过用户id和景点id删除景点收藏数据失败！");
        }
    }

    /**
     * @Description 通过用户id查询该用户收藏的所有景点信息【包括收藏时间】
     *
     * @Param userId
     * @Return java.util.Map<com.yoyo.itravel.bean.Sight , java.util.Date>  key：景点对象  value：景点收藏时间
     */
    @Override
    public Map<Sight, String> findAllCollectByUserId(String userId) throws SightCollectException {
        if (StringUtils.isEmpty(userId)) {
            throw new SightCollectException("景点id为空！");
        }

        // 构建Map集合
        Map<Sight, String> map = new HashMap<Sight, String>();

        try {
            // 通过用户id查询用户收藏的所有景点
            List<Sight> sights = sightDao.selectAllCollectByUserId(userId);
            // 遍历景点集合
            for (Sight sight : sights) {
                if (sight == null) {
                    continue;
                }

                // 通过景点id和用户id查找景点的收藏时间
                Date collectTime = sightCollectDao.selectCollectTimeByUserIdAndSightId(userId, sight.getSightId());

                map.put(sight, DateUtils.parseDateToStr(collectTime, DateFormatConstant.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCollectException("通过用户id和景点id查询景点收藏数据失败！");
        }
        return map;
    }

    /**
     * @Description 通过用户id和景点id查找景点收藏数据
     * @Param userId
     * @Param sightId
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> findSightCollect(String userId, String sightId) throws SightCollectException {
        // 非空判断，如果用户id和景点id都为空，则直接返回null
        if (StringUtils.isEmpty(userId) && StringUtils.isEmpty(sightId)) {
            throw new SightCollectException("用户id和景点id不能都为空！");
        }

        // 构建Map对象
        Map<String, Object> map = new HashMap<String, Object>();

        try {

            if (StringUtils.isEmpty(userId)) { // 通过景点id查找该景点的所有用户收藏数据
                sightCollectDao.selectUserIdAndCollectTimeBySightId(sightId);
            } else if (StringUtils.isEmpty(sightId)) { // 通过用户id查找该用户的所有景点收藏数据
                System.out.println("通过用户id查找该用户的所有景点收藏数据");
                Map<String, Object> sightIdAndCollectTimeMap = sightCollectDao.selectSightIdAndCollectTimeByUserId(userId);
                for (String s : sightIdAndCollectTimeMap.keySet()) {
                    if ("collectTime".equals(s)) {
                        // 将收藏时间设置到map集合中
                        map.put(s, sightIdAndCollectTimeMap.get(s));
                    } else {
                        // 通过景点id获得景点对象
                        Sight sight = sightDao.selectBySightId((String)sightIdAndCollectTimeMap.get(s));
                        // 将景点对象添加到map集合
                        map.put((String)sightIdAndCollectTimeMap.get(s), sight);
                    }
                }
            } else { // 通过用户id和景点id查找景点数据
                // 通过景点id和用户id获得景点收藏时间
                Date collectTime = sightCollectDao.selectCollectTimeByUserIdAndSightId(userId, sightId);
                // 判断是否查到数据
                if (collectTime == null) {
                    // 未查到数据
                    return null;
                }

                map.put("collectTime", collectTime);
            }

            // 返回
            return map;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new SightCollectException("过用户id和景点id查找景点数据失败！");
        }
    }

}
